package com.mj.scence.game.view
{
	import com.gamebase.net.rpc.baseTypeEx.int8;
	import com.mj.model.MjModel;
	import com.mj.vo.Card;
	import com.mj.vo.RoleData;
	
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.utils.setTimeout;
	
	import org.caurina.transitions.Tweener;
	
	import resLib.UI;
	
	import utils.core.ObjectPool;

	public class GameScenceV extends Sprite
	{
		private var _ui:MovieClip;
		private var myHeadLoader:Loader;
		private var otherHeadLoader:Loader;
		private var _model:MjModel;
		private var onBankerData:Array;
		private var onPosData:Array;
		private var isSelfBanker:Boolean;
		private var cardsInfo:Vector.<Card>;
		private var shaiziMc:MovieClip;//色子动画
		private var bankerMc:MovieClip;//显示庄家动画
		private var mjCardObverseBgBMD:BitmapData;
		private var mjCardCoverBgBMD:BitmapData;
		private var allCards:Vector.<MJCard>;
		private var hasChangePlayer:Boolean;
		
		private var myCardsSprite:Sprite;			//我的牌位置
		private var otherCardsSprite:Sprite;		//对方的牌位置
		private var myPutCardSprite:Sprite;		//我的出牌容器
		private var otherPutCardSprite:Sprite;		//对方的出牌容器
		
		private var mySwarmCardsPos:Array = [70, 500];	//我的打出的牌位置
		private var otherSwarmCardsPos:Array = [70, 15];	//对方的打出的牌位置
		
		private var myPlayCards:Vector.<MJCard>;
		private var otherPlayCards:Vector.<MJCard>;
		
		
		public function GameScenceV()
		{
			_ui = UI.getMovieClip("GameScenceUI", "mjScence");
			this.addChild(_ui);
			_ui.myUserPanel.ready.readyBtn.addEventListener(MouseEvent.CLICK, getReady);
			myCardsSprite = new Sprite();
			myCardsSprite.x = 200;
			myCardsSprite.y = 475;
			_ui.addChild(myCardsSprite);
			
			otherCardsSprite = new Sprite();
			otherCardsSprite.x = 200;
			otherCardsSprite.y = 15;
			_ui.addChild(otherCardsSprite);
			
			myPutCardSprite = new Sprite();
			myPutCardSprite.x = 200;
			myPutCardSprite.y = 345;
			_ui.addChild(myPutCardSprite);
			
			otherPutCardSprite = new Sprite();
			otherPutCardSprite.x = 200;
			otherPutCardSprite.y = 235;
			_ui.addChild(otherPutCardSprite);
			
			_model = MjModel.I;
			var mjCardObverseBg:MovieClip = UI.getMovieClip("ObverseCard", "mjScence");
			var mjCardCoverBg:MovieClip = UI.getMovieClip("UnGetCoverCard", "mjScence");
			mjCardObverseBgBMD = new BitmapData(mjCardObverseBg.width, mjCardObverseBg.height);
			mjCardObverseBgBMD.draw(mjCardObverseBg);
			mjCardCoverBgBMD = new BitmapData(mjCardCoverBg.width, mjCardCoverBg.height);
			mjCardCoverBgBMD.draw(mjCardCoverBg);
		}
		
		public function iniOtherUserPanel(roleData:RoleData, isReady:Boolean):void
		{
			hasChangePlayer = true;
			if(roleData == null)
			{
				_ui.otherUserPanel.visible = false;
				_ui.otherUserPanel.ready.visible = false;
			}
			else
			{
				_ui.otherUserPanel.userName.text = roleData.nickname;
				_ui.otherUserPanel.designation.gotoAndStop(roleData.title);
				if(otherHeadLoader)
				{
					if(otherHeadLoader.parent)
						otherHeadLoader.parent.removeChild(otherHeadLoader);
					otherHeadLoader.unloadAndStop();
				}
				else
				{
					otherHeadLoader = new Loader();
					otherHeadLoader.x = _ui.otherUserPanel.headPicMask.x;
					otherHeadLoader.y = _ui.otherUserPanel.headPicMask.y;
					otherHeadLoader.mask = _ui.otherUserPanel.headPicMask;
					_ui.otherUserPanel.addChild(otherHeadLoader);
				}
				//tt otherHeadLoader.load(new URLRequest(roleData.figureurl));
				if(isReady)
					_ui.otherUserPanel.ready.visible = true;
				else
					_ui.otherUserPanel.ready.visible = false;
				_ui.otherUserPanel.visible = true;
			}
			
		}
		
		public function iniSelfUserPanel(roleData:RoleData):void
		{
			resetMyUserPanel();
			_ui.myUserPanel.userName.text = roleData.nickname;
			_ui.myUserPanel.designation.gotoAndStop(roleData.title);
			if(myHeadLoader == null)
			{
				myHeadLoader = new Loader();
				myHeadLoader.x = _ui.myUserPanel.headPicMask.x;
				myHeadLoader.y = _ui.myUserPanel.headPicMask.y;
				myHeadLoader.mask = _ui.myUserPanel.headPicMask;
				_ui.myUserPanel.addChild(myHeadLoader);
				//tt myHeadLoader.load(new URLRequest(roleData.figureurl));
			}
			
		}
		
		
		private function resetMyUserPanel():void
		{
			_ui.myUserPanel.ready.gotoAndStop(1);
			_ui.myUserPanel.ready.visible = true;
		}
		
		private function getReady(evt:MouseEvent):void
		{
			_model.getReady();
		}
		
		public function selfReady():void
		{
			_ui.myUserPanel.ready.gotoAndStop(2);
		}
		
		public function otherReady():void
		{
			_ui.otherUserPanel.ready.visible = true;
		}
		
		public function onBanker(myNum:int, otherNum:int, $isSelfBanker:Boolean):void
		{
			onBankerData = [myNum, otherNum];
			isSelfBanker = $isSelfBanker;
		}
		
		public function onStartPos(myNum:int, otherNum:int):void
		{
			onPosData = [myNum, otherNum];
		}
		
		public function onGetCardInfo($cardsInfo:Vector.<Card>):void
		{
			cardsInfo = $cardsInfo;
			if(hasChangePlayer)
			{
				showShaizi(onBankerData[0], onBankerData[1]);
				flash.utils.setTimeout(showBankerMc, 3000);
			}
			else
			{
				flash.utils.setTimeout(showShaizi, 2000, onPosData[0], onPosData[1]);
				flash.utils.setTimeout(startTakeCard, 5000);
			}
		}
		
		/**
		 *游戏开始前生成盖着的牌 
		 */		
		public function layoutCoverCards():Vector.<MJCard>
		{
			var startX:int = 100;
			var otherCardY:int = 90;
			var selfCardY:int = 460;
			var layoutCard:MJCard;
			
			var i:int;
			var newCard:MJCard;
			if(allCards == null)
			{
				allCards = new Vector.<MJCard>();
				for(i=0;i<64;i++)
				{
					newCard = createCard();
					newCard.index = i;
					allCards.push(newCard);
				}
				
			}
			
			//对方案前的牌
			for(i=0; i<32; i++)
			{
				layoutCard = allCards[i];
				layoutCard.turnCover();
				layoutCard.rotation = 0;
				layoutCard.x = startX + int(layoutCard.width)*(i%16);
				if(i >= 16)
					layoutCard.y = otherCardY - 15;
				else
					layoutCard.y = otherCardY;
				_ui.addChild(layoutCard);
			}
			//自己案前的牌
			for(i=0;i<32;i++)
			{
				layoutCard = allCards[32+i];
				layoutCard.turnCover();
				layoutCard.rotation = 0;
				layoutCard.x = startX + int(layoutCard.width)*(16-(i%16)-1);
				if(i >= 16)
					layoutCard.y = selfCardY - 15;
				else
					layoutCard.y = selfCardY;
				_ui.addChild(layoutCard);
			}
			return allCards;
		}
		
		private function createCard():MJCard
		{
			var newCard:MJCard = ObjectPool.getObj("MJCard") as MJCard;
			if(newCard == null)
			{
				newCard = new MJCard(mjCardObverseBgBMD, mjCardCoverBgBMD);
				newCard.turnCover();
			}
			return newCard;
		}
		
		//播放色子动画
		private function showShaizi(myNum:int, otherNum:int):void
		{
			if(shaiziMc == null)
			{
				shaiziMc = UI.getMovieClip("ShaiziMc", "mjScence");
				shaiziMc.addEventListener("showMyNum", showMyNum);
				shaiziMc.addEventListener("showOtherNum", showOtherMyNum);
				
			}
			_ui.addChild(shaiziMc);
			shaiziMc.gotoAndPlay(2);
			
			function showMyNum(evt:Event):void
			{
				shaiziMc.my.gotoAndStop(myNum);
			}
			
			function showOtherMyNum(evt:Event):void
			{
				shaiziMc.other.gotoAndStop(otherNum);
			}
		}
		
		//播放庄家动画
		private function showBankerMc():void
		{
			if(bankerMc == null)
			{
				bankerMc =  UI.getMovieClip("BankerMc", "mjScence");
			}
			if(isSelfBanker)
			{
				bankerMc.x = _ui.myUserPanel.x - 200;
				bankerMc.y = _ui.myUserPanel.y - 50;
			}
			else
			{
				bankerMc.x = _ui.otherUserPanel.x - 200;
				bankerMc.y = _ui.otherUserPanel.y + 50;
			}
			_ui.addChild(bankerMc);
			bankerMc.gotoAndPlay(2);
			flash.utils.setTimeout(showShaizi, 2000, onPosData[0], onPosData[1]);
			flash.utils.setTimeout(startTakeCard, 5000);
		}
		
		
		
		//开始起牌
		private function startTakeCard():void
		{
			_ui.otherUserPanel.ready.visible = false;
			_ui.myUserPanel.ready.visible = false;
//			if(bankerMc && bankerMc.parent)
//			{
//				bankerMc.parent.removeChild(bankerMc);
//			}
			if(shaiziMc && shaiziMc.parent)
			{
				shaiziMc.parent.removeChild(shaiziMc);
			}
			this.dispatchEvent(new Event("startTakeCard"));
			
		}
		
		//各自起一张牌
		public function takePareCard(myCard:MJCard, otherCard:MJCard):void
		{
			var myCardValue:int = cardsInfo.pop().id.value;
			Tweener.addTween(myCard,{time:0.5, x:myCardsSprite.x+myCardsSprite.numChildren*myCard.width, y:myCardsSprite.y, onComplete:onTweenMyCard, onCompleteParams:[myCard, myCardValue]});
			Tweener.addTween(otherCard,{time:0.5, x:otherCardsSprite.x+otherCardsSprite.numChildren*otherCard.width, y:otherCardsSprite.y, onComplete:onTweenOtherCard, onCompleteParams:[otherCard]});
		}
		
		private function onTweenMyCard($card:MJCard, cardValue:int):void
		{
			$card.x = myCardsSprite.numChildren*$card.width;
			$card.y = 0;
			myCardsSprite.addChild($card);
			$card.turnObverse(cardValue);
			regMyCardEvent($card);
		}
		
		private function onTweenOtherCard($card:MJCard):void
		{
			$card.x = otherCardsSprite.numChildren*$card.width;
			$card.y = 0;
			otherCardsSprite.addChild($card);
		}
		
		//起一张牌
		public function takeOneCard(targetCard:MJCard, cardValue:int, isSelf:Boolean):void
		{
			var posX:int;
			var posY:int;
			if(isSelf)
			{
				posX = myCardsSprite.x + 415;
				posY = myCardsSprite.y;
				Tweener.addTween(targetCard,{time:0.5, x:posX, y:posY, onComplete:targetCard.turnObverse, onCompleteParams:[cardValue]});
				regMyCardEvent(targetCard);
			}
			else
			{
				posX = otherCardsSprite.x+otherCardsSprite.numChildren*targetCard.width;
				posY = otherCardsSprite.y;
				Tweener.addTween(targetCard,{time:0.5, x:posX, y:posY, onComplete:onTweenOtherCard, onCompleteParams:[targetCard]});
			}
		}
		
		private function regMyCardEvent($card:MJCard):void
		{
			$card.mouseEnabled = true;
			$card.addEventListener(MouseEvent.CLICK, selectCard);
		}
		
		private function releseMyCardEvent($card:MJCard):void
		{
			
		}
		
		//洗好牌后游戏开始
		public function gameBegin():void
		{
			hasChangePlayer = false;
		}
		
		private function selectCard(evt:MouseEvent):void
		{
			var $card:MJCard = evt.currentTarget as MJCard;
			$card.addEventListener(MouseEvent.CLICK, selfPutCard);
		}
		
		//自己出牌
		private function selfPutCard(evt:MouseEvent):void
		{
			var $card:MJCard = evt.currentTarget as MJCard;
			$card.removeEventListener(MouseEvent.CLICK, selfPutCard);
			$card.removeEventListener(MouseEvent.CLICK, selectCard);
			var sendCard:Card = new Card();
			sendCard.id = new int8($card.value);
			_model.selfPutCard(sendCard);
			myPutCardSprite
		}
									 
		//对方出牌
		public function onOtherPutCard(cardValue:int):void
		{
			
		}
		
		//游戏结束
		public function gameEnd():void
		{
			_ui.myUserPanel.ready.visible = true;
		}
		
		
	}
}